#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

#define DEBUG 0

// Flag vector to signal the end of the wins in the wins array
#define END_OF_WINS 0	

int columns;
int rows;
// NOTE: last_move starts at 1, not 0. This happens to be beneficial because, with padding, the first column is board[1]
int last_move;
int total_time;
int player_1_time;
int last_move_time;

// The Wins Lookup Table
char WINS_LOOKUP[12][4] = {"ggrr","grgr","rgrg","rrgg","grrg","rggr","ggbb","gbgb","bgbg","bbgg","gbbg","bggb"};

FILE *f;


/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
 and their point values
 
 This function has to do 3 things:
 1) figure out which player won - return positive or negative values
 2) Figure out what point value they won with - arrangment of the peices
 3) If two players have the same winning value - return 1 point
 */
int evalWin(char wins[150][4])
{
	
    int i = 0;
    int points = 0;
    int bestwin = 0;
    int worstwin = 0;
    int redpoints = 0;
    int bluepoints = 0;
    do{
        ////fprintf(f, f, "checking case %s\n", wins[i]);
        //check for tie - if there is nothing in the wins array
        if(wins[0][0] == '\0' || wins[0][0] == 0)
        {
            ////fprintf(f, "0");//no wins in array - no nothing in array
            points = 0;
            return 0;
        }
        else//we have at least one win - check it out
        {
            //first - check for the good wins - where the first two char's will match
            if(wins[i][0] == wins[i][1])//same colour first two pieces
            {
				
                if(wins[i][0] == 'r')
                {
                    redpoints += 5;
                    points = -5;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 5;
                    points = 5;
                }
				
                else if(wins[i][0] == 'g')//don't know who won yet
                {
                    if(wins[i][2] == 'r')
                    {
                        redpoints += 5;
                        points = -5;
                    }//we won
                    else
                    {
                        bluepoints += 5;
                        points = 5;
                    }
                }
            }
			
            else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
            {
				
                if(wins[i][0] == 'r')
                {
                    redpoints += 4;
                    points = -4;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 4;
                    points = 4;
                }
                else//first piece is green
                {//check the second piece
                    if(wins[i][1] == 'r')
                    {
                        redpoints += 4;
                        points = -4;
                    }//we won  
                    else if(wins[i][1] == 'b')
                    {
                        bluepoints += 4;
                        points = 4;
                    }
					
                }
				
            }
            else //3 points will be awarded
            {
                if(wins[i][0] == 'r')
                {
                    redpoints += 3;
                    points = -3;
                }//we won
                else if(wins[i][0] == 'b')
                {
                    bluepoints += 3;
                    points = 3;
                }
                else//first piece is green
                {//check the second piece
                    if(wins[i][1] == 'r')
                    {
                        redpoints += 3;
                        points = -3;
                    }//we won
                    else if(wins[i][1] == 'b')
                    {
                        bluepoints += 3;
                        points = 3;
                    }
                }
            }
			
			
			
        }
        if(points > 0)
        {
            if(bestwin < points)
                bestwin = points;
        }
        else
            if(worstwin > points)
                worstwin = points;
		
        i++;
    }while(wins[i][0] != 0);
	
	
    if( redpoints - bluepoints > 0)
        points = worstwin;
    else if(redpoints - bluepoints == 0)
        points = 1;
    else
        points = bestwin;
	
    ////fprintf(f,  "%d\n", points);
    return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
int readBoard(char board[columns+2][rows+2])
{
    int i, j, p = 0;
	
    // Populate board array
    for (i = 0; i < columns+2; i++)
    {
        // Padding columns on left and right sides of board
        if (i == 0 || i == columns+1)
        {
            for(j = 0; j < rows+2; j++)
                board[i][j] = SPACE;
            continue;
        }
        // Padding Row over bottom of board
        board[i][0] = SPACE;
        for (j = 1; j < rows+1; j++)
        {
            scanf(",%c", &board[i][j]);
            if(board[i][j] < 's')
				p++;
        }
        // Padding row over top of board
        board[i][j] = SPACE;
    }
    return p;
}

void findWins(char board[columns+2][rows+2], char wins[150][4], int last_played)
{
    // last_piece_x and last_piece_y hold co-ordinates of the last played piece, that is the piece that must be checked for wins
    int last_piece_x = last_played, last_piece_y, winIndex = 0;
    int i;
    // get the last piece's Y co-ordinate
    for (i=1;i<rows+1;i++)
    {
        if(board[last_piece_x][i] == SPACE)
            break;
        last_piece_y = i;
    }
	
    //fprintf(f, "Last Moved Piece's co-ordinates:%i, %i, %c\n", last_piece_x, last_piece_y, board[last_piece_x][last_piece_y]);  
	
    /*
     * How this works is: For each directional axis (X, Y, Diagonal Down Left-to-Right, and Diagonal Up Left-to-Right), the last dropped
     * piece can be in 4 vectors (it can be in positions 0-3). For each of these possible positions, the algorithm grabs the 4 pieces
     * and then checks that vector to see if it is a win. If so, it adds it to the wins array.
     */
	
    // Diagonal Top Left<->Bottom Right
    //fprintf(f, "Beginning Diagonal Loop\n");
    for(i=0;i<4;i++) // Check each of the 4 possible positions for the last dropped piece
    {
        short int offset;
        short int size = 1;
		
        //fprintf(f, "for position:%i\n", i);
		
        // The co-ordinates used to build the vector to check on. Note these move diagonally to the right,down.
        short int start_piece_x = last_piece_x + i;
        short int start_piece_y = last_piece_y - i;
        //fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        // If the starting piece is a space, stop moving in that direction
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            //fprintf(f, "BREAK1: Found Space, finished looking at diagonal!\n", i);
            break;
        }
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x-offset][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }  
            win[size++] = board[start_piece_x-offset][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // Top<->Bottom
    ////fprintf(f, "Beginning Vertical Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
        short int start_piece_x = last_piece_x, start_piece_y = last_piece_y - i;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at Vertical!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
            win[size++] = board[start_piece_x][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // Diagonal Bottom Left<->Top Right
    ////fprintf(f, "Beginning 2nd Diagonal Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
		
        short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y - i;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at 2nd diagonal!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x+offset][start_piece_y+offset] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
			
            win[size++] = board[start_piece_x+offset][start_piece_y+offset];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    //  Left<->Right
    ////fprintf(f, "Beginning Horizontal Loop\n");
    for(i=0;i<4;i++)
    {
        short int offset, size = 1;
        ////fprintf(f, "for position:%i\n", i);
		
        short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y;
        ////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
        if (board[start_piece_x][start_piece_y] == SPACE)
        {
            ////fprintf(f, "BREAK1: Found Space, finished looking at Horizontal!\n", i);
            break;
        }
		
        char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
        // Build a vector going diagonally to the up, left
        for (offset = 1;offset < 4;offset++)
        {
            if (board[start_piece_x+offset][start_piece_y] == SPACE)
            {
                ////fprintf(f, "BREAK2: Found Space While building a vector\n");
                break;
            }
			
            win[size++] = board[start_piece_x+offset][start_piece_y];
        }
        if (size == 4) // Found a four piece vector (No Spaces)
        {
            ////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
            int k;
            for (k=0;k<12;k++)
            {
                if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
                {
                    ////fprintf(f, "Found a Win! Vector is %s\n", win);
                    strcpy(wins[winIndex++], win);
                }
            }
			
        }
    }
    // End with "end of wins" flag vector, to signal the end of wins
    wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+2])
{
#if DEBUG
    int i, j;
    // TEST: Print out board
    for (i = 0; i < rows+2; i++)
    {
        fprintf(f, "\t");
        for (j = 0; j < columns+2; j++)
        {
            fprintf(f, "%c ", board[j][i]);
        }
        fprintf(f, "\n");
    }
    fprintf(f, "\n");
#endif
}

int isWin(char board[columns+2][rows+2], int last_played)
{
    char wins[150][4];
    findWins(board, wins, last_played);
    return evalWin(wins);
	
}

// guess() is the controller for the heuristics. guess() builds all possible vectors for the board and then calls the heuristic(s) for each and totals the score, which is returned.
//(10,7,7,2156,0,443,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,b,g,r,s,s,s,s,b,b,r,s,s,s,s,b,r,g,r,s,s,s,r,r,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
int guess(char board[columns+2][rows+2])
{
	/*
		REMEMBER: 	Any changes made to how a vector is processed must be applied to all four directions.
	*/

	// Variables
	int x, y; // X and Y Co-Ordinates of each piece
	int xi, yi; // X and Y Offset Co-Ordinates for building the vector on piece X,Y
	int i; // 4 Piece Loop Counter

	int count = 0; // For debugging, a count of how many vectors are grabbed and checked

	/* Score Variables ***************************************************/
	int score; 	// The total score for the board
	int score_heuristic_1 = 0; // The Heuristic score for this board configuration

	// TODO: Add score variables for other heuristics here

	/*********************************************************************/

	// Go Through Every Piece (or Space) and check each of the four possible vectors built on that piece
	for (x = 1;x < columns + 1;x++)
	{
		for (y = 0; y < rows + 1;y++)
		{
			char vector[4] = {0, 0, 0, 0};	// The vector being built
			int break_flag = 0;	// Flag signifying that vector has reached edge of board and will be skipped

			// 1) Straight Up (Y+1)
			// Reset offsets
			xi = x;
			yi = y;
			
			vector[0] = board[x][y];		

			for (i = 1;i < 4; i++)
			{
				// Update offsets
				yi++;
				
				// Check Break condition
				if (yi > rows)
				{
					break_flag = 1;
					break;
				}
				
				// Add Piece to Vector
				vector[i] = board[xi][yi];
			}
			if (!break_flag)	// Vector has been built successfully, now get its score
			{
				//printf("Built a Vector: %s\n", vector);
				count++;
				/* Call heuristics *********************************/
				score_heuristic_1 += heuristic_1(vector);

				// TODO: Call additional Heuristics here
				
				/***************************************************/
			}

			// 2) Diagonal Up, Left to Right (X+1,Y+1)
			// Reset offsets and break flag
			break_flag = 0;
			xi = x;
			yi = y;

			// Reset the Vector
			vector[0] = board[x][y];
			vector[1] = 0;
			vector[2] = 0;
			vector[3] = 0;			

			for (i = 1;i < 4; i++)
			{
				// Update offsets
				xi++;
				yi++;

				// Check Break condition
				if ((yi > rows) || (xi > columns))
				{
					break_flag = 1;
					break;
				}
				
				// Add Piece to Vector
				vector[i] = board[xi][yi];
			}
			if (!break_flag)	// Vector has been built successfully, now get its score
			{
				//printf("Built a Vector: %s\n", vector);
				count++;
				/* Call heuristics *********************************/
				score_heuristic_1 += heuristic_1(vector);

				// TODO: Call additional Heuristics here
				
				/***************************************************/
			}

			// 3) Straight Right (X+1)
			// Reset offsets and break flag
			break_flag = 0;
			xi = x;
			yi = y;

			// Reset the Vector
			vector[0] = board[x][y];
			vector[1] = 0;
			vector[2] = 0;
			vector[3] = 0;

			for (i = 1;i < 4; i++)
			{
				// Update offsets
				xi++;

				// Check Break condition
				if (xi > columns)
				{
					break_flag = 1;
					break;
				}
				
				// Add Piece to Vector
				vector[i] = board[xi][yi];
			}
			if (!break_flag)	// Vector has been built successfully, now get its score
			{
				//printf("Built a Vector: %s\n", vector);
				count++;
				/* Call heuristics *********************************/
				score_heuristic_1 += heuristic_1(vector);

				// TODO: Call additional Heuristics here
				
				/***************************************************/
			}

			// 4) Diagonal Down, Left to Right (X+1, Y-1)
			// Reset offsets and break flag
			break_flag = 0;
			xi = x;
			yi = y;

			// Reset the Vector
			vector[0] = board[x][y];
			vector[1] = 0;
			vector[2] = 0;
			vector[3] = 0;

			for (i = 1;i < 4; i++)
			{
				// Update offsets
				xi++;
				yi--;

				// Check Break condition
				if ((xi > rows)||(yi < 1))
				{
					break_flag = 1;
					break;
				}
				
				// Add Piece to Vector
				vector[i] = board[xi][yi];
			}
			if (!break_flag)	// Vector has been built successfully, now get its score
			{
				//printf("Built a Vector: %s\n", vector);
				count++;
				/* Call heuristics *********************************/
				score_heuristic_1 += heuristic_1(vector);

				// TODO: Call additional Heuristics here
				
				/***************************************************/
			}
		}
	}
	//printf("\n\nTotal Vector count for %i X %i board: %i\n", columns, rows, count);
	
	/* TODO: Combine all heuristic scores together here. *****************/
	// Could do a weighted average, if there is a heuristic we trust more than others.
	score = score_heuristic_1;
	/*********************************************************************/

	return score;
}

// First Heuristic. Uses Lookup table to get the isWin value of the vector, then weights the result and returns a score
int heuristic_1(char vector[4])
{
	/* Heuristic Weights **************************/
	const int WIN_MULTIPLIER = 1000000;
	const int ONE_AWAY_MULTIPLIER = 100000;
	const int TWO_AWAY_MULTIPLIER = 10000;
	/**********************************************/

	// TODO: Call Byte->Int lookup table for vector here
	int vectorScore = 0;
				
	// Scale vectorScore for Heuristic
	if (vectorScore >= 3 || vectorScore <= -3)
		vectorScore *= WIN_MULTIPLIER;
	else if (vectorScore == 2 || vectorScore == -2)
		vectorScore *= ONE_AWAY_MULTIPLIER;
	else if (vectorScore == 1 || vectorScore == -1)
		vectorScore *= TWO_AWAY_MULTIPLIER;

	return vectorScore;	
}

/*
 *
 *	Tested and works correctly
 *
 */

int checkCol(char board[columns+2][rows+2], int col)
{  
    int i = 0;
	
    if(board[col][rows] != SPACE)
        return 0;//if the top of the col isn't a space - it is full
    else
        for(i = rows-1; i > 0; i--)
        {
            if(board[col][i] == SPACE)
                continue;
            else
                return i+1;
        }
    return i+1;
}
// (8,7,5,0,0,0,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//g,s,s,s,s,s,s,
//g,b,s,s,s,s,s,
//s,s,s,s,s,s,s,
//b,g,s,s,s,s,s)

//Implements Minimax search - lowest level returns the min, then the upper levels take the
//max value of the returned mins
int minMax( char board[columns+2][rows+2], int depth)
{
    int max_depth = 5;
   
    max(board, 5);  
}

int max(char board[columns+2][rows+2], int depth){
   
    int best = -5, bestcol = 0;
    int val = 0;
    int i = 0;
    int row = 0;
   
    if(depth <= 0)
        return 0;
   
    for(i = 1; i < columns+1; i++)
    {
        if((row = checkCol(board, i)) == 0)
            continue;//col is full
        board[i][row] = BLUE;
//		printBoard(board);
        val = isWin(board, i)
        if(val == 5)
        	return 5 * pow(10, depth);
        else
        	val += val * pow(10, dpeth) + min(board, depth-1);
        if(val > best)
        {
            best = val;
            bestcol = i;
        }
       
        board[i][row] = GREEN;
#if DEBUG
		fprintf(f, "depth = %d, col = %d, isWin = %d\n", depth, i, isWin(board, i));

//		printBoard(board);
#endif
        val = isWin(board, i)
        if(val < -2 || val  == 5 )
        	return val*pow(10, depth);
        else
        	val += val * pow(10, depth) + min(board, depth-1);
        if(val > best)
        {
            best = val;
            bestcol = i;
        }
       board[i][row] = SPACE;
    }
   
    return pow(10, depth) * best;
   
   
}

int min(char board[columns+2][rows+2], int depth)
{
    int best = 5, bestcol = 0;
    int val = 0;
    int i = 0;
    int row = 0;
   
    if(depth <= 0)
        return 0;
   
    for(i = 1; i < columns+1; i++)
    {
        if((row = checkCol(board, i)) == 0)
            continue;//col is full
        board[i][row] = RED;
//		printBoard(board);
        val = isWin(board, i);
        if(val == -5)
        	return -5 * pow(10, depth);
        else
        	val += val * pow(10, dpeth) + max(board, depth-1);
        if(val < best)
        {
            best = val;
            bestcol = i;
        }
       
        board[i][row] = GREEN;
//		printBoard(board);
        val = isWin(board, i);
        if(val == -5)
        	return -5 * pow(10, depth);//if opponent gets they best win, stop
        else if(val > 2)//assume opponent won't play a green to help us...
        	continue;
        else
        	val += val * pow(10, dpeth) + max(board, depth-1);
        if(val < best)
        {
            best = val;
            bestcol = i;
        }
		board[i][row] = SPACE;
       
    }
   
    return pow(10, depth) * best;
}

int tryMove(char board[columns+2][rows+2], int col, char piece)
{
    int i = 0, row = -1, depth = 6;
    // First, see if column has room for another piece
    //printBoard(board);
   
    if((row = checkCol(board, col)) == 0)
        return -99999;
    else
    {
        board[col][row] = piece;
//		printBoard(board);
        //rank the moves by the number of points we can get to
        int score = pow(10, depth) * isWin(board, col) + max(board, depth-1);
        board[col][row] = SPACE;
        return score;
    }
   
}

int makeMove(char board[columns+2][rows+2])
{
    int  i,tmp, max_move = -1, min_move = -1, max = 0, min = 0;
    char piece;
    // Loop through all possible moves
    for(i=1;i<=columns;i++)
    {
       
        // Try placing a Blue piece
        tmp = tryMove(board, i, BLUE);
#if DEBUG
        fprintf(f, "Trying col: %d - Piece: Blue - SCORE: %d\n\n", i, tmp);
#endif
        if (tmp > max)
        {
            max_move = i;
            piece = BLUE;
            max = tmp;               
        }
        // Try placing a Green piece
        tmp = tryMove(board, i, GREEN);
#if DEBUG
        fprintf(f, "Trying col: %d - Piece: Green - SCORE: %d\n\n", i, tmp);
#endif
        if (tmp > max)
        {
            max_move = i;
            piece = GREEN;
            max = tmp;               
        }
        else if (tmp < min)
        {
            if(tmp == -99999)//row is full
                continue;
            min_move = i;
            min = tmp;               
        }
        // Simulate the opponent placing Red here
        tmp = tryMove(board, i, RED);
#if DEBUG
        fprintf(f, "Trying col: %d - Piece: Red - SCORE: %d\n\n", i, tmp);
#endif
        if (tmp < min)
        {
            if(tmp == -99999)//row is full
                continue;
            min_move = i;
            min = tmp;               
        }                 
    }
    /*if(min < 0) // There's a Opponent's Win to Block
     fprintf(f, "(%d, %c)", min_move, BLUE);
     
     else
     */if(max > 0) // There's a Win
         printf("(%d, %c)", max_move, piece);
    if(min < 0)
        printf("(%d, b)", min_move);
	if(max == 0)
		printf("(%d, b)", last_move);
   
   
   
}


int checkLoss(char board[columns+2][rows+2])
{
    int i = 0, j = 0, row = 0, score = 0, max = 0, max_move = 0;
    char piece;
#if DEBUG
//    printBoard(board);
#endif
   
    for(i = 1; i <= columns; i++)
    {
        // First, see if column has room for another piece
        for(j=1;j<=rows;j++)
        {
            if(board[i][j] == SPACE)
            {
                row = j;
                break;             
            }               
        }
        if (row == -1);
        else
        {
            board[i][row] = RED;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
           
            //if we are going to lose right away, place a blue to block;
            if(score < 0)
            {
                printf("(%d, b)", i);
                return 1;
            }
           
            board[i][row] = BLUE;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
           
            //if we are going to lose right away, place a blue to block;
            if(score > 0)
            {
                if(score > max)
                {
                    max_move = i;
                    max = score;
                    piece = BLUE;
                }
            }
           
            board[i][row] = GREEN;
            //rank the moves by the number of points we can get to
            score = isWin(board, i);
#if DEBUG
            fprintf(f, "Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
            board[i][row] = SPACE;
            if(score < 0)
            {
                printf("(%d, b)", i);
                return 1;
            }
            else if(score > max)
            {
                max_move = i;
                piece = GREEN;
                max = score;
            }
           
        }
        if(max)
        {
            printf("(%d, %c)", max_move, piece);
            return 1;
        }
    }
    return 0; 
}

//(10,7,5,0,0,0,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
//(10,7,7,2156,0,443,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,b,g,r,s,s,s,s,b,b,r,s,s,s,s,b,r,g,r,s,s,s,r,r,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
int main( int argc, char **argv)
{
   
    int p = 0;
#if DEBUG
    char fname[32];
#endif
   
    scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
#if DEBUG
    sprintf(fname, "./moves/game.out.%d.txt", player_1_time);
    f = fopen(fname, "w");
#endif
    char board[columns+2][rows+2];
    p = readBoard(board);
   
/*********************************************/
	// For Debugging the Heuristic -Nick
//	guess(board);
//	return 0;
/********************************************/
    if(checkLoss(board))
    {
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
   
   
    if(p == 0)
    {
        printf("(5, b)");
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
    else if(p < 2)
    {
        printf("(%d, b)", last_move+1);
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
    else if(p < 6)
    {
        printf("(%d, b)", last_move);
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
	else if(p < 9)
    {
        printf("(%d, b)", last_move-1);
#if DEBUG
        fclose(f);
#endif
        return 0;
    }
   
    makeMove(board);
#if DEBUG
    fclose(f);
#endif
    return 0;
}


